package org.jtzc.springaiagent.agent.collaborative;

import org.jtzc.springaiagent.agent.BaseAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wu chuang
 * @description
 */
@Service
public class CollaborativeAgentGroup implements BaseAgent {
    private final List<BaseAgent> specialists;
    private final BaseAgent coordinator;

    @Autowired
    public CollaborativeAgentGroup(
            @Qualifier("researchAgent") BaseAgent researchAgent,
            @Qualifier("writingAgent") BaseAgent writingAgent,
            @Qualifier("editingAgent") BaseAgent editingAgent,
            @Qualifier("coordinatorAgent") BaseAgent coordinator) {

        this.specialists = List.of(researchAgent, writingAgent, editingAgent);
        this.coordinator = coordinator;
    }

    @Override
    public AgentResponse execute(AgentRequest request) {
        // 并行执行专家代理
        List<AgentResponse> specialistResponses = specialists.parallelStream()
                .map(agent -> agent.execute(request))
                .collect(Collectors.toList());

        // 协调结果
        AgentRequest consolidationRequest = new AgentRequest(
                "Consolidate the following responses for the query: " + request.prompt() +
                        "\nResponses: " + specialistResponses.stream()
                        .map(AgentResponse::content)
                        .collect(Collectors.joining("\n---\n")),
                request.context()
        );

        return coordinator.execute(consolidationRequest);
    }
}
